package com.asl.website.serviceContent.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.asl.website.serviceContent.domain.SysServiceContentDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.asl.common.annotation.Log;
import com.asl.common.core.controller.BaseController;
import com.asl.common.core.domain.AjaxResult;
import com.asl.common.enums.BusinessType;
import com.asl.website.serviceContent.domain.SysServiceContent;
import com.asl.website.serviceContent.service.ISysServiceContentService;
import com.asl.common.utils.poi.ExcelUtil;
import com.asl.common.core.page.TableDataInfo;

/**
 * 服务内容Controller
 * 
 * @author ruoyi
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/serviceContent/serviceContent")
public class SysServiceContentController extends BaseController
{
    @Autowired
    private ISysServiceContentService sysServiceContentService;
    /**
     * 获取所有服务内容列表（按排序）
     */
    @GetMapping("/content/{typeId}")
    public AjaxResult getAllContents(
            @PathVariable("typeId") Integer typeId,
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage) {
        SysServiceContent query = new SysServiceContent();
        query.setTypeId(typeId); // 设置 typeId 到查询对象
        List<SysServiceContent> contents = sysServiceContentService.selectSysServiceContentList(query);
        if (contents.isEmpty()) {
            return AjaxResult.error("未配置任何服务内容");
        }

        List<SysServiceContentDTO> result = contents.stream()
                .map(content -> new SysServiceContentDTO(content, acceptLanguage))
                .sorted(Comparator.comparingInt(SysServiceContentDTO::getSortOrder))
                .collect(Collectors.toList());

        return AjaxResult.success(result);
    }
    /**
     * 查询服务内容列表
     */
    @PreAuthorize("@ss.hasPermi('serviceContent:serviceContent:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysServiceContent sysServiceContent)
    {
        startPage();
        List<SysServiceContent> list = sysServiceContentService.selectSysServiceContentList(sysServiceContent);
        return getDataTable(list);
    }

    /**
     * 导出服务内容列表
     */
    @PreAuthorize("@ss.hasPermi('serviceContent:serviceContent:export')")
    @Log(title = "服务内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysServiceContent sysServiceContent)
    {
        List<SysServiceContent> list = sysServiceContentService.selectSysServiceContentList(sysServiceContent);
        ExcelUtil<SysServiceContent> util = new ExcelUtil<SysServiceContent>(SysServiceContent.class);
        util.exportExcel(response, list, "服务内容数据");
    }

    /**
     * 获取服务内容详细信息
     */
    @PreAuthorize("@ss.hasPermi('serviceContent:serviceContent:query')")
    @GetMapping(value = "/{contentId}")
    public AjaxResult getInfo(@PathVariable("contentId") Long contentId)
    {
        return success(sysServiceContentService.selectSysServiceContentByContentId(contentId));
    }

    /**
     * 新增服务内容
     */
    @PreAuthorize("@ss.hasPermi('serviceContent:serviceContent:add')")
    @Log(title = "服务内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysServiceContent sysServiceContent)
    {
        return toAjax(sysServiceContentService.insertSysServiceContent(sysServiceContent));
    }

    /**
     * 修改服务内容
     */
    @PreAuthorize("@ss.hasPermi('serviceContent:serviceContent:edit')")
    @Log(title = "服务内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysServiceContent sysServiceContent)
    {
        return toAjax(sysServiceContentService.updateSysServiceContent(sysServiceContent));
    }

    /**
     * 删除服务内容
     */
    @PreAuthorize("@ss.hasPermi('serviceContent:serviceContent:remove')")
    @Log(title = "服务内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contentIds}")
    public AjaxResult remove(@PathVariable Long[] contentIds)
    {
        return toAjax(sysServiceContentService.deleteSysServiceContentByContentIds(contentIds));
    }
}
